package com.haibei.adapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashSet;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

import com.haibei.R;
import com.haibei.adapter.bean.CollectArticle;
import com.haibei.adapter.bean.Message;

public class MessageAdapter extends BaseAdapter {

	private Context mContext;
	private ArrayList<Message> mArrayList;
	private boolean isEdit = false;
	private HashSet<Message> mHashSetSelect;

	public MessageAdapter(Context context, ArrayList<Message> datalist) {
		this.mContext = context;
		this.mArrayList = datalist;
		this.mHashSetSelect = new HashSet<Message>();
	}

	public void removeSelect() {
		ArrayList<Message> needDletes=new ArrayList<Message>();
		for (Message msg : mArrayList) {
			if (mHashSetSelect.contains(msg)) {
				needDletes.add(msg);
			}
		}
		mArrayList.removeAll(needDletes);
		mHashSetSelect.clear();
		notifyDataSetChanged();
	}
	
	public HashSet<Message> getSelectSet(){
		return mHashSetSelect;
	}
	
	@Override
	public int getCount() {
		return mArrayList.size();
	}

	@Override
	public Object getItem(int position) {
		return mArrayList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder vl = null;
		if (null == convertView) {
			vl = new ViewHolder();
			convertView = View.inflate(mContext, R.layout.item_messge_item, null);
			vl.textViewTitle = (TextView) convertView.findViewById(R.id.textViewTitle);
			vl.textViewContent = (TextView) convertView.findViewById(R.id.textViewContent);
			vl.textViewTime = (TextView) convertView.findViewById(R.id.textViewTime);
			vl.relativeLayoutRadio = convertView.findViewById(R.id.relativeLayoutRadio);
			vl.checkBoxDel = (CheckBox) convertView.findViewById(R.id.checkBoxDel);
			convertView.setTag(vl);
		} else {
			vl = (ViewHolder) convertView.getTag();
		}
		final Message msg = mArrayList.get(position);

		vl.textViewTitle.setText(msg.getTitle());
		vl.textViewContent.setText(msg.getContent());
		String time = "";
		try {
			long t = Long.parseLong(msg.getCrateTime());
			Calendar ca = Calendar.getInstance();
			ca.setTimeInMillis(t);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
			time = sdf.format(ca.getTime());
		} catch (Exception e) {
			e.printStackTrace();
		}
		vl.textViewTime.setText(time);
		// ====
		if (isEdit) {
			vl.relativeLayoutRadio.setVisibility(View.VISIBLE);
			vl.checkBoxDel.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					if (isChecked) {
						mHashSetSelect.add(msg);
					} else {
						mHashSetSelect.remove(msg);
					}
				}
			});
			if (mHashSetSelect.contains(msg)) {
				vl.checkBoxDel.setChecked(true);
			}
		} else {
			vl.relativeLayoutRadio.setVisibility(View.GONE);
			vl.checkBoxDel.setOnCheckedChangeListener(null);
		}

		return convertView;
	}

	public void setEditEable(boolean isEdit) {
		this.isEdit = isEdit;
		notifyDataSetChanged();
	}

	public boolean isEdit() {
		return isEdit;
	}

	class ViewHolder {
		TextView textViewTitle;
		TextView textViewContent;
		TextView textViewTime;
		View relativeLayoutRadio;
		CheckBox checkBoxDel;
	}

}
